<?php

namespace app\admin\controller;

use think\Controller;
use app\admin\model\Admin;

class Base extends Controller{
    public function _initialize(){
        // 监听
        parent::_initialize();
        $session_admin_id = session('ADMIN_ID');
      
        if (empty($session_admin_id)) {
            if ($this->request->isPost()) {
                $this->error("您还没有登录！", url("login/index"));
            } else {
                header("Location:" . url("login/index"));
                exit();
            }
        } else {
            $admin = new Admin();
            $user = $admin->getUserById($session_admin_id);
            //if (!$this->checkAccess($session_admin_id)) {
            //    $this->error("您没有访问权限！");
            //}
            $this->assign("admin", $user);

        }
    }

    /**
     *  检查后台用户访问权限
     * @param int $userId 后台用户id
     * @return boolean 检查通过返回true
     */
    private function checkAccess($userId){
        // 如果用户id是1，则无需判断
        if ($userId == 1) {
            return true;
        }

        $module     = $this->request->module();
        $controller = $this->request->controller();
        $action     = $this->request->action();
        $rule       = $module . $controller . $action;

        $notRequire = ["adminIndexindex", "adminMainindex"];
        if (!in_array($rule, $notRequire)) {
            //return cmf_auth_check($userId);
            return true;
        } else {
            return true;
        }
    }
}